package lastRites.game.networking;

import java.util.logging.Level;
import java.util.logging.Logger;

import lastRites.entities.Player;
import lastRites.enums.GameStatus;
import lastRites.game.LastRites;
import lastRites.game.networking.NetworkPackets.ChatPacket;
import lastRites.game.networking.NetworkPackets.GameJoinPacket;
import lastRites.game.networking.NetworkPackets.GameStartPacket;
import lastRites.game.networking.NetworkPackets.LoginRequest;
import lastRites.game.networking.NetworkPackets.LoginResponse;
import lastRites.game.networking.NetworkPackets.MapBlockPacket;
import lastRites.game.networking.NetworkPackets.PlayerInfoPacket;
import lastRites.game.networking.NetworkPackets.PlayerListPacket;
import lastRites.game.networking.NetworkPackets.PlayerMovementResponse;
import lastRites.game.networking.NetworkPackets.PlayerMovementUpdate;
import lastRites.game.networking.NetworkPackets.TextDisplayPacket;

import org.newdawn.slick.geom.Vector2f;

import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;

public class ClientListener extends Listener {
	Logger	logger	= Logger.getLogger( "ClientListener" );

	@Override
	public void connected(Connection connection) {
		connection.sendTCP( new LoginRequest( LastRites.THIS_PLAYER.getName(), "", connection.getRemoteAddressTCP().getAddress().getHostAddress() ) );
	}

	@Override
	public void received(Connection connection, Object object) {
		logger.setLevel( Level.OFF );
		if( object instanceof ChatPacket ) {
			logger.info( "Received a chat packet" );
			LastRites.CHAT_MESSAGES.add( ( (ChatPacket)object ).text );
			if( LastRites.CHAT_MESSAGES.size() > 10 ) {
				LastRites.CHAT_MESSAGES.remove();
			}
		}
		if( object instanceof TextDisplayPacket ) {
			logger.info( "Received a text packet" );
			LastRites.TEXT_ON_SCREEN.add( (TextDisplayPacket)object );
		}
		if( object instanceof LoginResponse ) {
			logger.info( "Received a login request. " + ( (LoginResponse)object ).response );
			if( ( (LoginResponse)object ).response ) {
				LastRites.GAME_STATUS = GameStatus.LOGGINGON;
			} else {
				LastRites.GAME_STATUS = GameStatus.PROBLEMCONNECTING;
			}
		}
		if( object instanceof GameJoinPacket ) {
			logger.info( "Received a game join packet" );
			// References.STATE = ConnectionState.JOININGGAME;
			// References.CHOSEN_GAME_STATE = GameState.values()[( (GameJoinPacket)object ).gameStateId];
			logger.info( "Process game join packet." );
		}
		if( object instanceof GameStartPacket ) {
			logger.info( "Received a game start packet." );
			LastRites.GAME_STATUS = GameStatus.INGAME;
			LastRites.THIS_PLAYER.setPosition( ( (GameStartPacket)object ).x, ( (GameStartPacket)object ).y );
		}
		if( object instanceof PlayerInfoPacket ) {
			logger.info( "Received a player info packet" );
			PlayerInfoPacket packet = (PlayerInfoPacket)object;
			LastRites.THIS_PLAYER = packet.player;
			LastRites.THIS_PLAYER.setCurrentWorld( LastRites.CURRENT_WORLD );
			LastRites.PLAYER_LIST.add( LastRites.THIS_PLAYER );
		}
		if( object instanceof MapBlockPacket ) {
			MapBlockPacket packet = (MapBlockPacket)object;
			LastRites.CURRENT_WORLD.getBlocks()[packet.block.getRow()][packet.block.getCol()] = packet.block;
		}
		if( object instanceof PlayerMovementUpdate ) {
			PlayerMovementUpdate packet = (PlayerMovementUpdate)object;
			logger.info( "Received a player movement update packet " + packet.player + " moving to X:" + packet.x + ", Y:" + packet.y );
			for( Player player : LastRites.PLAYER_LIST ) {
				if( player.getName().equalsIgnoreCase( packet.player ) ) {
					player.setPosition( packet.x, packet.y );
				}
			}
		}
		if( object instanceof PlayerMovementResponse ) {
			PlayerMovementResponse packet = (PlayerMovementResponse)object;
			logger.info( "Received a player movement response packet " + packet.response );
			if( packet.response ) {
				if( LastRites.THIS_PLAYER.getMovementList().size() > 1 ) {
					LastRites.THIS_PLAYER.getMovementList().remove();
				}
				LastRites.THIS_PLAYER.setPosition( LastRites.THIS_PLAYER.getProposedPlayerX(), LastRites.THIS_PLAYER.getProposedPlayerY() );
				logger.info( "Moved us to X:" + LastRites.THIS_PLAYER.getPosition().x + " Y:" + LastRites.THIS_PLAYER.getPosition().y );
			} else {
				if( LastRites.THIS_PLAYER.getMovementList().size() > 1 ) {
					Vector2f lastKnownGood = LastRites.THIS_PLAYER.getMovementList().get( 0 );
					LastRites.THIS_PLAYER.setPosition( lastKnownGood.x, lastKnownGood.y );
				}
			}
			logger.info( "Resetting pendingValidation" );
			LastRites.THIS_PLAYER.setPendingValidation( false );
		}
		if( object instanceof PlayerListPacket ) {
			logger.info( "Received a player list packet." );
			PlayerListPacket packet = (PlayerListPacket)object;
			for( Player player : packet.players ) {
				if( !player.getName().equalsIgnoreCase( LastRites.THIS_PLAYER.getName() ) ) {
					LastRites.PLAYER_LIST.add( player );
				}
			}
		}
	}
}
